package cn.ad.service;


import cn.ad.common.DataUtils;
import cn.ad.exception.AgencyValidateException;
import cn.ad.handler.SqlSessionHandler;
import cn.ad.interceptor.PageParameter;
import cn.ad.model.AgencyInfoModel;
import cn.ad.model.AgencyModel;
import cn.ad.model.DropDownModel;
import cn.ad.entity.Agency;
import cn.ad.model.ResultSetList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class AgencyService {
	
	@Autowired
	private SqlSessionHandler sqlSessionHandler;
	
	public Agency readAgencyById(Integer agencyId) {
		// TODO Auto-generated method stub
		return sqlSessionHandler.selectOne("agencyMapper.agency:select:one", agencyId);
	}


	public AgencyInfoModel addAgency(Agency agency) {
		if (isRepeate(agency)){
			throw new AgencyValidateException(AgencyValidateException.ErrorCode.AGENCY_REPEAT);
		}
		sqlSessionHandler.insert("agencyMapper.insert",agency);
		return findAgencyById(agency.getId());
	}

	public AgencyInfoModel findAgencyById(Integer id) {
		AgencyInfoModel model=sqlSessionHandler.selectOne("agencyMapper.selectById",id);
		if (model==null){
			throw new AgencyValidateException(AgencyValidateException.ErrorCode.AGENCY_NOTFOUND);
		}
		return model;
	}

	public AgencyInfoModel editAgency(Agency agency) {
		if (isRepeate(agency)){
			throw new AgencyValidateException(AgencyValidateException.ErrorCode.AGENCY_REPEAT);
		}
		sqlSessionHandler.update("agencyMapper.updateById",agency);
		return findAgencyById(agency.getId());
	}

	public ResultSetList<AgencyModel> findAgencies(Map<String, Object> searchMap) {
		Integer offset= Integer.parseInt(searchMap.get("offset").toString());
		Integer limit= Integer.parseInt(searchMap.get("limit").toString());
		PageParameter page=new PageParameter(offset,limit);
		List<AgencyModel> list=sqlSessionHandler.selectList("agencyMapper.select:list:page",searchMap,page);
		ResultSetList<AgencyModel> result=new ResultSetList<>(page.getTotalCount(),list);
		return result;
	}

	public List<DropDownModel> getAgencies() {
		return sqlSessionHandler.selectList("agencyMapper.select:dropdown",null);
	}

	private boolean isRepeate(Agency agency) {
		return (Integer) sqlSessionHandler.selectOne("agencyMapper.select:checkname", agency) > 0;
	}
	/**
	 * 更新代理商余额
	 * @param agency
     */
	public boolean updateAgencyAmount(Agency agency,boolean isPlus) {
		if(null == agency || 0 == agency.getId()) {
			throw new AgencyValidateException(AgencyValidateException.ErrorCode.AGENCY_NOTFOUND);
		}
		Agency agencyObj = readAgencyById(agency.getId());
		if(null == agencyObj) {
			throw new AgencyValidateException(AgencyValidateException.ErrorCode.AGENCY_NOTFOUND);
		}
		if(!isPlus) {
			if(0 == agencyObj.getBalanceCash()) {
				throw new AgencyValidateException(AgencyValidateException.ErrorCode.AGENCY_AMOUNT);
			}
			if(!DataUtils.compare(new BigDecimal(agencyObj.getBalanceCash()),new BigDecimal(agency.getBalanceCash()))) {
				throw new AgencyValidateException(AgencyValidateException.ErrorCode.AGENCY_AMOUNT);
			}else {
				agencyObj.setBalanceCash(new Double(DataUtils.sub(new BigDecimal(agencyObj.getBalanceCash()),new BigDecimal(agency.getBalanceCash())).doubleValue()));
			}
		}else {
			agencyObj.setBalanceCash(new Double(DataUtils.add(new BigDecimal(agencyObj.getBalanceCash()),new BigDecimal(agency.getBalanceCash())).doubleValue()));
		}
		sqlSessionHandler.update("agencyMapper.update", agencyObj);
		return true;
	}
}
